package dao.impl;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

import dao.PatientDAO;
import model.Patient;

public class PatientDAOImpl implements PatientDAO{

	private HashMap<String, Patient> patients;
	private int 	SeqId = 0;
    private int 	version = 1;
	
    public PatientDAOImpl() {
    	if (patients.size() == 0) {
			add("DAO_IMPL_NII_1", "firstname1", "lastname1","Notes for Patient1");
			add("DAO_IMPL_NII_2", "firstname2", "lastname2","Notes for Patient2");
			add("DAO_IMPL_NII_3", "firstname3", "lastname3","Notes for Patient3");
		}
   	    System.out.println("Creating Patient DAO Implementation");
    }
 
    // create
	public boolean add(String NII, String firstName, String lastName,
			String notes) {
		Patient p = new Patient(null, version, NII, firstName, lastName, notes);
		return add(p);

	}
	public boolean add(Patient patient) {
	  	patient.setId(SeqId);
		patients.put(patient.getNII(), patient);
        SeqId++;
		return true;
	}
	
    public Patient[] getAll() {
    	Collection<Patient> collection = patients.values();
		return collection.toArray(new Patient[collection.size()]);
	}	
       
  	public long getCount() {
		return patients.size();
	}
	
  	public Patient getById(Integer id) {
		return patients.get(id);
	}

	public Patient getByNII(String NII) {
		Patient patient = null;
		for (Patient p : patients.values()) {
			if (p.getNII().equals(NII)) {
				patient = p;
				break;
			}
		}
		return patient;
	}
	
	public Patient[] getByFirstName(String name) {
		List<Patient> found = new ArrayList<Patient>();
		for (Patient p : patients.values()) {
			if (p.getFirstName().equals(name)) {
				found.add(p);
			}
		}
		return found.toArray(new Patient[found.size()]);
	}

	public Patient[] getByLastName(String name) {
		List<Patient> found = new ArrayList<Patient>();
		for (Patient p : patients.values()) {
			if (p.getLastName().equals(name)) {
				found.add(p);
			}
		}
		return found.toArray(new Patient[found.size()]);
	}
	
	public boolean update(Patient patient) {
    	patients.put(patient.getNII(), patient);
		return false;
	}
	
	public boolean delete(Patient patient) {
		patients.remove(patient.getNII());
	    return true;
	}
	
    public Patient save(Patient patient) {
    	patient.setId(SeqId);
    	patients.put(patient.getNII(), patient);
        SeqId++;
		return patient;
	}
  
}
    